package com.icode.web.validator;


import com.icode.core.dto.house.HouseBaseDTO;
import com.icode.core.model.house.HouseBase;
import com.icode.core.model.house.HouseBaseImage;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-16
 * Time: 上午9:17
 * To change this template use File | Settings | File Templates.
 */
@Component
public class HouseBaseFormValidator implements Validator {

    @Autowired
    private HouseService houseService;

    @Override
    public boolean supports(Class<?> clazz) {
        return HouseBaseDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        HouseBaseDTO houseBaseDTO = (HouseBaseDTO) target;
        String guid = houseBaseDTO.getGuid();
        String title = houseBaseDTO.getTitle();
        String introduction = houseBaseDTO.getIntroduction();
        String information = houseBaseDTO.getInformation();
        String price = houseBaseDTO.getPrice();
        String productType = houseBaseDTO.getProductType();
        List<MultipartFile> images = houseBaseDTO.getImages();
        if (!"".equals(houseBaseDTO.getId())) {
            List<HouseBaseImage> houseBaseImages = houseService.findHouseBaseImageByHouseBaseId(houseBaseDTO.getId());
            List<String> imageGuids = HouseBaseDTO.initImageGuids(houseBaseImages);
            houseBaseDTO.setImageGuids(imageGuids);
        }

        if (!isNull(title, errors) || !isNull(introduction, errors) || !isNull(information, errors) || !isNull(price, errors) || !isNull(productType, errors)) {
            return;
        }
        if (isExistTitle(title, guid)) {
            errors.rejectValue("title", null, "该名字已被使用!");
        }
        if (!imgIsNull(images)) {
            if (!isImages(images)) {
                errors.rejectValue("images", null, "图片格式不正确!");
            }
            if (isLarge(images)) {
                errors.rejectValue("images", null, "图片不能超过5M!");
            }
        }


    }


    private static boolean isNull(String s, Errors errors) {
        if ("".equals(s)) {
            return false;
        }
        return true;
    }

    private boolean isImages(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if (isEquals(m.getOriginalFilename())) {
                return true;
            }
        }
        return false;
    }

    private boolean isLarge(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if (m.getSize() > 5120000) {
                return true;
            }
        }
        return false;
    }

    private boolean isEquals(String fileName) {
        String[] file = fileName.split("\\.");
        String[] fileType = {"jpg", "png", "psd", "jpeg", "gif"};
        for (String type : fileType) {
            if (type.equalsIgnoreCase(file[file.length - 1])) {
                return true;
            }
        }
        return false;
    }

    private boolean isExistTitle(String title, String guid) {
        List<HouseBase> houseBases = houseService.findHouseBaseByTitle(title);
        if (!houseBases.isEmpty()) {
            if (houseBases.size() >= 1 && !houseBases.get(0).guid().equals(guid)) {
                return true;
            }
        }
        return false;
    }

    private boolean imgIsNull(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if ("".equalsIgnoreCase(m.getOriginalFilename())) {
                return true;
            }
        }
        return false;
    }

}
